package com.iamalive;


import com.iamalive.LocalCache.RecipientDetails;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Context;

public class UpdateRecipientActivity extends Activity {
	
	private EditText mEditTextName;
	private EditText mEditTextEmail;
	private EditText mEditTextPhone;
	private EditText mEditTextAddress;
	private EditText mEditTextRelation;
	private Button mButtonUpdate;
	private Button mButtonDelete;
	private Button mButtonCancel;
	
	private String mName;
	private String mEmail;
	private String mPhone;
	private String mAddress;
	private String mRelation;
	
	private Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_recipient);
		
		mEditTextName = (EditText) findViewById(R.id.editTextUpdateRecipientName);
		mEditTextEmail = (EditText) findViewById(R.id.editTextUpdateRecipientEmail);
		mEditTextPhone = (EditText) findViewById(R.id.editTextUpdateRecipientPhone);
		mEditTextAddress = (EditText) findViewById(R.id.editTextUpdateRecipientAddress);
		mEditTextRelation = (EditText) findViewById(R.id.editTextUpdateRecipientRelationship);
		mButtonUpdate = (Button) findViewById(R.id.buttonUpdateRecipientUpdate);
		mButtonDelete = (Button) findViewById(R.id.buttonUpdateRecipientDelete);
		mButtonCancel = (Button) findViewById(R.id.buttonUpdateRecipientCancel);
		
		mContext = getApplicationContext();
		
		mName = "";
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			mName = extras.getString("Recipient Name");
		}
		
		populateFieldsWithExistingValues();
		
		mButtonUpdate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onUpdateButtonClicked();
            }
        });
		
		mButtonDelete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onDeleteButtonClicked();
            }
        });
		
		mButtonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
	}


	private void populateFieldsWithExistingValues() {
		LocalCache localCache = LocalCache.getSingleInstance();
		RecipientDetails recipientDetails = localCache.mRecipientDetailsMap.get(mName);
		
		mEditTextName.setText(recipientDetails.name);
		mEditTextEmail.setText(recipientDetails.email);
		mEditTextPhone.setText(recipientDetails.phone);
		mEditTextAddress.setText(recipientDetails.address);
		mEditTextRelation.setText(recipientDetails.relationship);
	}
	
	private void onUpdateButtonClicked() {		
		mEmail = mEditTextEmail.getText().toString();
		mPhone = mEditTextPhone.getText().toString();
		mAddress = mEditTextAddress.getText().toString();
		mRelation = mEditTextRelation.getText().toString();
		//TODO: Update via web service
		
		//Update into the localcache too
		LocalCache localCache = LocalCache.getSingleInstance();
		RecipientDetails recipientDetails = localCache.mRecipientDetailsMap.get(mName);
		recipientDetails.email = mEmail;
		recipientDetails.phone = mPhone;
		recipientDetails.address = mAddress;
		recipientDetails.relationship = mRelation;
		
		Utils.showToast(mContext, "Recipient details have been updated into our records");
		finish();
	}
	
	private void onDeleteButtonClicked() {
		//TODO : update via web service
		
		LocalCache localCache = LocalCache.getSingleInstance();
		//Remove all references from info list of localcache
		for(int i = 0; i < localCache.mInfoList.size(); i++) {
			localCache.mInfoList.get(i).listOfRecipients.remove(mName);
		}	

		//Remove from recipientMap of localcache;		
		localCache.mRecipientDetailsMap.remove(mName);
		
		String message = "Recipient " + mName + " has been deleted from our records";
		
		Utils.showToast(mContext, message);
		finish();
	}
}
